//
//  NSMutableArray+Safe.m
//  mmbang
//
//  Created by 倪元政 on 16/7/7.
//  Copyright © 2016年 iyaya. All rights reserved.
//

#import "NSMutableArray+Safe.h"

@implementation NSMutableArray (Safe)

#pragma  mark 增加

- (BOOL)safeAddObject:(id)anObject {
    if (![self isKindOfClass:[NSArray class]]) {
        return NO;
    }
    if (anObject) {
        [self addObject:anObject];
        return YES;
    } else {
        return NO;
    }
}

- (BOOL)safeInsertObject:(id)anObject atIndex:(NSUInteger)index {
    if (![self isKindOfClass:[NSMutableArray class]]) {
        return NO;
    }
    if (anObject) {
        if (index <= [self count]) {
            [self insertObject:anObject atIndex:index];
            return YES;
        } else {
            return NO;
        }
        
    } else {
        return NO;
    }
}

#pragma mark 删除

- (BOOL)safeRemoveObjectAtIndex:(NSInteger)index {
    if (![self isKindOfClass:[NSMutableArray class]]) {
        return NO;
    }
    if (index < [self count] && index >= 0) {
        [self removeObjectAtIndex:index];
        return YES;
    } else {
        return NO;
    }
}

#pragma mark 查询

- (id)safeObjectAtIndex:(NSUInteger)index {
    if (![self isKindOfClass:[NSArray class]]) {
        return nil;
    }
    if (index<[self count]) {
        return [self objectAtIndex:index];
    }else{
        return nil;
    }
}

#pragma mark 更新

- (BOOL)safeReplaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject {
    if (![self isKindOfClass:[NSArray class]]) {
        return NO;
    }
    if (!anObject) {
        return NO;
    }
    if (index < self.count) {
        [self replaceObjectAtIndex:index withObject:anObject];
        return YES;
    } else {
        return NO;
    }
}

@end
